﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInfo
{
    public class WI_HonorTypeDAL
    {
        public List<WI_HonorType> GetAllHonorType()
        {
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.WI_HonorType.AsQueryable();
            List<WI_HonorType> honorlist = query.ToList();
            return honorlist;
        }

        public List<WI_HonorType> GetAllHonorType(int start, int limit, out int ttlCount)
        {
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.WI_HonorType.OrderBy(c => c.HonorTypeID);
            ttlCount = query.Count();
            List<WI_HonorType> honorlist = query.Skip(start).Take(limit).ToList();
            return honorlist;
        }

        public WI_HonorType GetHonorType(int honTypeId)
        {
            SugardbEntities ctx = new SugardbEntities();
            WI_HonorType honType1 = ctx.WI_HonorType.FirstOrDefault(c => c.HonorTypeID == honTypeId);
            return honType1;
        }

        public int CreateHonorType(string honTypeName)
        {
            int honTypeId = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_HonorType newhorType = new WI_HonorType
                {
                    HonorTypeName=honTypeName
                };
                ctx.WI_HonorType.AddObject(newhorType);
                ctx.SaveChanges();
                honTypeId = newhorType.HonorTypeID;
            }
            return honTypeId;
        }

        public bool UpdateHonorType(int honTypeId, string honTypeName)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_HonorType wihonType = ctx.WI_HonorType.FirstOrDefault(c => c.HonorTypeID == honTypeId);
                if (wihonType != null)
                {
                    wihonType.HonorTypeName = honTypeName;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveHonorType(int honTypeId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_HonorType wiaItem = ctx.WI_HonorType.FirstOrDefault(c => c.HonorTypeID == honTypeId);
                if (wiaItem != null)
                {
                    ctx.WI_HonorType.DeleteObject(wiaItem);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
